﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace Jerry.DroidBuilder.Controls
{
    public class WaitLock : IDisposable
    {
        private static int _lockedInstace = 0;

        private WaitLock()
        {
            // Prevents others to create instance.
            //
            _lockedInstace++;

            var viewModel = (Application.Current.MainWindow as MainWindow).ViewModel;

            viewModel.IsLocked = true;
        }

        public static WaitLock Acquire()
        {
            return new WaitLock();
        }

        public void Dispose()
        {
            _lockedInstace--;

            if (_lockedInstace <= 0)
            {
                _lockedInstace = 0;

                var viewModel = (Application.Current.MainWindow as MainWindow).ViewModel;

                viewModel.IsLocked = false;
            }
        }
    }
}
